$(function () {
    $("#jqGrid").jqGrid({
        url: '../appdownload/list',
        datatype: "json",
        colModel: [			
			{ label: 'avId', name: 'avId', index: 'av_id', width: 50, key: true },
			{ label: 'app最新版本号', name: 'appLatestVersionCode', index: 'app_latest_version_code', width: 80 }, 			
			{ label: 'app版本名称', name: 'appVersionName', index: 'app_version_name', width: 80 }, 			
			{ label: '手机操作系统（ios，android）', name: 'mobileSystem', index: 'mobile_system', width: 80 }, 			
			{ label: 'app下载地址', name: 'appDownAddr', index: 'app_down_addr', width: 80 }, 			
			{ label: 'app描述信息', name: 'appDescription', index: 'app_description', width: 80 }, 			
			{ label: '是否强制升级（0否，1是）', name: 'isMandatoryUpgrade', index: 'is_mandatory_upgrade', width: 80 }, 			
			{ label: '信息录入时间', name: 'infoEntryTime', index: 'info_entry_time', width: 80 }, 			
			{ label: '状态', name: 'status', index: 'status', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });


	new AjaxUpload('#upload', {
		action: '../sys/oss/upload',
		name: 'file',
		autoSubmit:true,
		responseType:"json",
		onSubmit:function(file, extension){
			if (!(extension && /^(jpg|jpeg|png|gif|bin|apk)$/.test(extension.toLowerCase()))){
				alert('只支持jpg、png、gif、bin格式的文件！');
				return false;
			}
		},
		onComplete : function(file, r){
			if(r.code == 0){
				$("#downloadPath").val(r.url);
			}else{
				alert(r.msg);
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		appDownload: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.appDownload = {};
		},
		update: function (event) {
			var avId = getSelectedRow();
			if(avId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(avId)
		},
		saveOrUpdate: function (event) {
			var url = vm.appDownload.avId == null ? "../appdownload/save" : "../appdownload/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.appDownload),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var avIds = getSelectedRows();
			if(avIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../appdownload/delete",
				    data: JSON.stringify(avIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(avId){
			$.get("../appdownload/info/"+avId, function(r){
                vm.appDownload = r.appDownload;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});